package com.ccp.dev.form.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.form.model.FormCombinationDialog;
import com.ccp.dev.form.service.FormCombinationDialogService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 自定义对话框-组合对话框控制器类
 *
 * @author chenjing
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_FORM + "/formDialog/combinationDialog")
@Action(ownermodel = SysAuditModelType.FORM_MANAGEMENT)
public class FormCombinationDialogController extends SysBaseController {

    @Resource
    private FormCombinationDialogService formCombinationDialogService;

    /**
     * 数据列表页面跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/list")
    @Action(description = "组合对话框数据列表页面跳转",exectype = "页面跳转日志")
    public String combinationDialogViewList() {
        return getAutoView();
    }

    /**
     * 预览页面跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/preview")
    @Action(description = "组合对话框预览页面跳转",exectype = "页面跳转日志")
    public String combinationDialogViewPreview(HttpServletRequest request, ModelMap modelMap) {
        String combinationAlias = RequestUtil.getString(request,"combinationAlias");
        String isOpenJsonData = RequestUtil.getString(request,"isOpenJsonData");
        Boolean isOpenShowDialog = RequestUtil.getBoolean(request,"isOpenShowDialog");
        modelMap.put("combinationAlias",combinationAlias);
        modelMap.put("isOpenJsonData",isOpenJsonData);
        String retUrl = getAutoView();
        if (isOpenShowDialog) {
            retUrl =  "form/formDialog/combinationDialog/showPreview";
        }
        return retUrl;
    }

    /**
     * 树结构预览页面跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/treeStructure")
    @Action(description = "组合对话框树结构预览页面跳转",exectype = "页面跳转日志")
    public String combinationDialogViewTreeStructure() {
        return getAutoView();
    }
    /**
     * 树结构预览多选页面跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/treeStructureCheckbox")
    @Action(description = "组合对话框树结构预览多选页面跳转",exectype = "页面跳转日志")
    public String combinationDialogViewTreeStructureCheckbox() {
        return getAutoView();
    }
    /**
     * 选择对话框弹窗列表页面跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/dialogChoice")
    @Action(description = "组合对话框选择对话框弹窗列表页面跳转",exectype = "页面跳转日志")
    public String combinationDialogViewDialogChoice(HttpServletRequest request, ModelMap modelMap) {
        String dialogType = RequestUtil.getString(request,"dialogType");
        String dialogId = RequestUtil.getString(request,"dialogId");
        modelMap.put("dialogId",dialogId);
        modelMap.put("dialogType",dialogType);
        return getAutoView();
    }
    /**
     * 新增及编辑页面跳转
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/addDialog")
    @Action(description = "组合对话框新增及编辑页面跳转",exectype = "页面跳转日志")
    public String combinationDialogViewAdd(HttpServletRequest request, ModelMap modelMap) {
        String combinationId = RequestUtil.getString(request,"combinationId");
        modelMap.put("combinationId",combinationId);
        return getAutoView();
    }

    /**
     * 取得组合对话框的分页列表
     *
     * @param request HttpServletRequest
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description = "查询组合对话框分页列表",exectype = "查询日志",detail = "查询组合对话框分页列表<#if success>成功<#else>失败</#if>")
    public ResultData combinationDialogList(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, true);
        try {
            List<FormCombinationDialog> formDialogCombinationList = formCombinationDialogService.queryList(queryFilter);
            SysAuditThreadLocalHolder.putParamerter("success",true);
            return getResultDataSuccess("query.success", queryFilter.getPageBean().getTotalCount(), formDialogCombinationList);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return getResultDataError("query.failure");
        }
    }

    /**
     * 组合对话框编辑
     *
     * 2019-11-4 新增别名查询
     * 考虑到更多地方是使用别名查询组合对话框，所以在id 查询的基础上添加别名查询
     *
     * @param request
     * @throws Exception
     */
    @RequestMapping("/edit")
    @ResponseBody
    @Action(description = "查询组合对话框编辑信息",exectype = "查询日志",detail = "查询组合对话框编辑信息<#if success>成功<#else>失败</#if>")
    public ResultData detail(HttpServletRequest request){
            String combinationAlias = RequestUtil.getString(request,"alias");
            String combinationId = RequestUtil.getString(request,"combinationId");
            try {
                FormCombinationDialog formCombinationDialog;
                if(StringUtil.isNotEmpty(combinationAlias)) {
                    formCombinationDialog = formCombinationDialogService.getByAlias(combinationAlias);
                }else {
                    formCombinationDialog = formCombinationDialogService.getOneById(combinationId);
                }
                SysAuditThreadLocalHolder.putParamerter("success",true);
                return getResultDataSuccess("query.success", formCombinationDialog);
            }catch (Exception e){
                SysAuditThreadLocalHolder.putParamerter("success",false);
                return getResultDataError("query.failure");
            }
    }

    /**
     * 组合对话框添加
     *
     * @param formCombinationDialog
     * @throws Exception
     */
    @RequestMapping("/add")
    @ResponseBody
    @Action(description = "添加或更新组合对话框",detail = "添加与更新组合对话框<#if success>成功<#else>失败</#if>")
    public ResultData add(@RequestBody FormCombinationDialog formCombinationDialog){
            try {
                if(BeanUtils.isEmpty(formCombinationDialog.getId())){
                    String alias = formCombinationDialog.getAlias();
                    if(formCombinationDialogService.aliasExist(alias)){
                        return getResultDataError("dataSource.save.aliasWarning");
                    }
                }
                formCombinationDialogService.save(formCombinationDialog);
                SysAuditThreadLocalHolder.putParamerter("success",true);
                return getResultDataSuccess("save.success");
            }catch (Exception e){
                e.printStackTrace();
                SysAuditThreadLocalHolder.putParamerter("success",false);
                return getResultDataError("save.failure");
            }

    }

    /**
     * 删除组合对话框
     *
     * @param request
     * @throws Exception
     */
    @RequestMapping("/del")
    @ResponseBody
    @Action(description = "删除组合对话框",detail = "删除组合对话框<#if success>成功<#else>失败</#if>")
    public ResultData del(HttpServletRequest request){
        try {
            String[] ids = RequestUtil.getStringAryByStr(request, "ids");
            formCombinationDialogService.delByIds(ids);
            SysAuditThreadLocalHolder.putParamerter("success",true);
            return getResultDataSuccess("delete.success");
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return getResultDataError("delete.failure");
        }

    }

    /**
     * 选择组合对话框（取得所有组合对话框）
     *
     * @return 组合对话框列表
     */
    @RequestMapping("/getAllCombinationDialogs")
    @Action(description = "查询组合对话框列表", exectype = "查询日志",
            detail = "查询组合对话框列表<#if success>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData getAllCombinationDialogs() {
        try {
            List<FormCombinationDialog> list = formCombinationDialogService.getAllCombinationDialogs();
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("query.success", list);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("query.failure");
        }
    }
}
